#_*_coding:UTF-8_*_
#import re
import os
#import time

#请修改以下参数，包名和工作空间脚本可以自动获取到，请将此脚本放置于包目录下面，否则获取路径会出错
#jarname为生成jar的名字，可随意
#ID为 android平台的编号，通过 android list 来查询，取android-19，如果没有则请复制一份到plarform目录下
#TestCase 为用例对应的类名，注意无需加包名
#请安装pydev插件到您的eclipse中和安装python2.7到您电脑中

#***********************************************
jarname="smoke"
ID="4"
TestCase="PowerCase#testDelay"
#**********************************************


print '----------Start debug-----------'
#get workspace path
curpath=os.getcwd()
index=curpath.index('src')
workspace=curpath[0:index-1]
print 'The current workspace path: ',workspace

#get current package
lenth=len(curpath)
packageTmp=curpath[index+4:lenth]
packageName=packageTmp.replace("\\",".")
package=packageName+"."+TestCase
print 'The current package: ',packageName
print 'The current package+class: ',package

print "----Start uiautomator debug----"
print "Create build.xml file"

os.system("android create uitest-project -n "+jarname+" -t "+ID+" -p "+"\""+workspace+"\"")

print "-Start modify the build.xml file-"
o=open(workspace+"\\build.xml","rb+")
d=""
for s in o.readlines():   
    a=s.replace("help","build")
    d+=a
o.seek(0)      
o.truncate
o.write(d)
o.close()       
print "-Modify the build.xml file is successfully-"

print "-ANT BUILD-"
os.system("ant -buildfile "+"\""+workspace+"\\build.xml"+"\"")
#time.sleep(1)

print "-push file-"
os.system("adb push "+"\""+workspace+"\\bin\\"+jarname+".jar"+"\""+" /data/local/tmp/")
#time.sleep(1)

print "-RUN-"
os.system("adb shell uiautomator runtest "+jarname+".jar"+" -c "+package)
print "----------finish deubg-------------"
#Have any questions,please contact "fanhq1@lenovo.com"
